package com.blockmeta.bbs.immodule.widget.msgview;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.ImageView;

import com.blockmeta.bbs.baselibrary.utils.CacheUtils;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.corebean.IMMetaBase;
import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.blockmeta.bbs.immodule.widget.RoundProgressBarWidthNumber;
import com.chad.library.adapter.base.BaseViewHolder;
import com.liulishuo.filedownloader.FileDownloader;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class FileShareMsgView<T extends ChatMessage> extends MsgView<T>{
    IMMetaBase mImMetaBase;


    public FileShareMsgView(Context context) {
        super(context);
        mImMetaBase = IMApplicationContext.getIMMetaService();
    }

    @Override
    public int getLayoutId() {
        return R.layout.im_chat_file;
    }

    @Override
    public void convert(final BaseViewHolder helper, final T item, T lastItem) {
        super.convert(helper, item, lastItem);
        super.bindCommonControl(helper, item, lastItem);

        String desc = "";
        try {
            JSONObject jsonObject = new JSONObject(item.getFileParam());
            long size = jsonObject.getLong("size");
//            if(size < 1 * 1024 * 1024){
//                startDownLoadMessageFile(item);
//            }
            desc = CacheUtils.getFormatSize(size);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        int fileTypeDrawable = getFileTypeDrawable(item.getFileType());
        //发送消息！
        if (item.getType() == ChatMessage.TYPE_SEND) {
            ImageView icon = helper.getView(R.id.right_img_icon);
            icon.setImageResource(fileTypeDrawable);
            helper.setText(R.id.right_title, item.getFileName());
            helper.setText(R.id.right_desc, desc);


            if(TextUtils.isEmpty(item.getFilePath())){
                //上传中
                helper.setGone(R.id.right_img_icon, false);
                helper.setGone(R.id.right_progressbar, true);
                RoundProgressBarWidthNumber rightProgress = helper.getView(R.id.right_progressbar);
                rightProgress.setProgress(mImMetaBase.getFileProgress(item.getContent()));
            }else if(TextUtils.isEmpty(item.getContent()) && mImMetaBase.getFileProgress(item.getFilePath()) != 0) {
                //文库文件下载中
                helper.setGone(R.id.right_img_icon, false);
                helper.setGone(R.id.right_progressbar, true);
                RoundProgressBarWidthNumber rightProgress = helper.getView(R.id.right_progressbar);
                rightProgress.setProgress(mImMetaBase.getFileProgress(item.getFilePath()));

            }else {
                helper.setGone(R.id.right_img_icon, true);
                helper.setGone(R.id.right_progressbar, false);
            }

            helper.addOnClickListener(R.id.chat_file_layout_content);
        } else {
            //接收消息
            ImageView icon = helper.getView(R.id.left_img_icon);
            icon.setImageResource(fileTypeDrawable);
            helper.setText(R.id.left_title, item.getFileName());
            helper.setText(R.id.left_desc, desc);

            if(TextUtils.isEmpty(item.getContent()) && mImMetaBase.getFileProgress(item.getFilePath()) != 0){
                //文件下载中
                helper.setGone(R.id.left_img_icon, false);
                helper.setGone(R.id.left_progressbar, true);
                RoundProgressBarWidthNumber leftProgress = helper.getView(R.id.left_progressbar);
                leftProgress.setProgress(mImMetaBase.getFileProgress(item.getFilePath()));
            }else {
                helper.setGone(R.id.left_img_icon, true);
                helper.setGone(R.id.left_progressbar, false);
            }

            helper.addOnClickListener(R.id.left_chat_file_layout_content);
        }

    }

    private int getFileTypeDrawable(int fileType) {
        int drawId = R.drawable.im_file;
        if(fileType == ChatMessage.TYPE_XLS || fileType == ChatMessage.TYPE_XLSX){
            drawId = R.drawable.im_excel;
        }else if(fileType == ChatMessage.TYPE_DOC || fileType == ChatMessage.TYPE_DOCX){
            drawId = R.drawable.im_word;
        }else if(fileType == ChatMessage.TYPE_PPT || fileType == ChatMessage.TYPE_PPTX){
            drawId = R.drawable.im_ppt;
        }else if(fileType == ChatMessage.TYPE_PDF){
            drawId = R.drawable.im_pdf;
        }
        return drawId;
    }

    byte getFileDownLoadStatus(T item){
        String externalStoragePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
        final String downLoadFileName = externalStoragePublicDirectory + File.separator + item.getFileName();

        FileDownloader.setup(mContext);
        byte status = FileDownloader.getImpl().getStatus(item.getFilePath(), downLoadFileName);
        return status;
    }



}
